#include<iostream>
#include<cstdio>
using namespace std;
int n,m,k,a;
int score[351],card[5];
int f[42][42][42][42];
int readint()
{
	int i=0,f=1;
	char ch;
	for(ch=getchar();(ch<'0'||ch>'9') && ch!='-';ch=getchar());
	if(ch=='-')
	{
		f=-1; 
		ch=getchar();
	}
	for(;ch>='0' && ch<='9';ch=getchar())
		i=(i<<3)+(i<<1)+ch-'0';
	return i*f;
}
int main()
{
	n=readint();
	m=readint();
	for(int i=1;i<=n;i++) score[i]=readint();
	for(int i=1;i<=m;i++){k=readint();card[k]++;}
	f[0][0][0][0]=score[1];	
	for(int i=0;i<=card[1];i++)
	for(int j=0;j<=card[2];j++)
	for(int k=0;k<=card[3];k++)
	for(int p=0;p<=card[4];p++)
	{
		a=1+i+2*j+3*k+4*p;
		if(i) f[i][j][k][p]=max(f[i][j][k][p],f[i-1][j][k][p]+score[a]);
		if(j) f[i][j][k][p]=max(f[i][j][k][p],f[i][j-1][k][p]+score[a]);
		if(k) f[i][j][k][p]=max(f[i][j][k][p],f[i][j][k-1][p]+score[a]);
		if(p) f[i][j][k][p]=max(f[i][j][k][p],f[i][j][k][p-1]+score[a]);
	}
	cout<<f[card[1]][card[2]][card[3]][card[4]];
	return 0;
}
